//
//  ServiceEvaluationCell.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/23.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "ServiceEvaluationCell.h"
#import "ServiceEvaluationModel.h"
#import "ShopEvaluationEvaluateInfosModel.h"
#import "ShopEvaluationAssociationModel.h"
@interface ServiceEvaluationCell()
@property (weak, nonatomic) IBOutlet UIImageView *iv;
@property (weak, nonatomic) IBOutlet UILabel *nickNamed;
@property (weak, nonatomic) IBOutlet UILabel *contentL;
@property (weak, nonatomic) IBOutlet UILabel *timeL;
@property (weak, nonatomic) IBOutlet UIButton *giveLick;
@property (weak, nonatomic) IBOutlet UIImageView *subIV;
@property (weak, nonatomic) IBOutlet UILabel *subTitle;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;
//@property(nonatomic,strong) MCButton *likeBtn;
@property(nonatomic,strong) NSArray *images;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *subViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *likeBtnConstraint;
@property (weak, nonatomic) IBOutlet UIButton *complaintBtn;

@end
@implementation ServiceEvaluationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.iv.layer.masksToBounds = YES;
    self.iv.layer.cornerRadius = 20;
    self.iv.layer.borderColor  = [UIColor colorWithRed:187/255.0 green:187/255.0 blue:187/255.0 alpha:1].CGColor;
    self.iv.layer.borderWidth  = 1;
    self.giveLick.layer.masksToBounds = YES;
    self.giveLick.layer.cornerRadius = 21/2;
    self.giveLick.layer.borderColor  = kLineColor.CGColor;
    self.giveLick.layer.borderWidth  = 1;
    [self.giveLick setTitleColor:[UIColor colorWithRed:162/255.0 green:162/255.0 blue:162/255.0 alpha:1] forState:UIControlStateNormal];
    [self.giveLick setTitleColor:kOrangeBg forState:UIControlStateSelected];
    self.manager = [AFHTTPSessionManager manager];
    self.subTitle.textColor = kLabel102Color;
}
#pragma mark-代理:点赞回调
-(void)updateSupportCount:(NSNotification *)info
{
   
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark-代理:点赞
- (IBAction)giveLike:(UIButton *)sender {
    
    UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([[UserDefaultsHelper readAutoLogIn] isEqualToString:@"NO"]||[UserDefaultsHelper readAutoLogIn].length == 0) {
        LogInOrOutVC *logVC = [[LogInOrOutVC alloc]init];
        MainNavC *mainNav = [[MainNavC alloc]initWithRootViewController:logVC];
        [viewController presentViewController:mainNav animated:YES completion:nil];
    }else {
        self.dict = @{
                      @"userId":[UserDefaultsHelper readUid],
                      @"token":[UserDefaultsHelper readToken]
                      };
        self.urlString = [NSString stringWithFormat:@"%@/user/checkToken",kUrl];
        [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSInteger status = [[responseObject objectForKey:@"status"] integerValue];
            if (status == 200) {
                if ([self.delegate respondsToSelector:@selector(serviceEvaluationCell:withGiveLikeBtn:withIndexPath:withShopEvaluationEvaluateInfosModel:)]) {
                    [self.delegate serviceEvaluationCell:self withGiveLikeBtn:self.giveLick withIndexPath:self.indexPath withShopEvaluationEvaluateInfosModel:self.shopEvaluationEvaluateInfosModel];
                }
                
            }else {
                LogInOrOutVC *logVC = [[LogInOrOutVC alloc]init];
                MainNavC *mianNav = [[MainNavC alloc]initWithRootViewController:logVC];
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"警告", @"Prompt") message:NSLocalizedString(@"您的账号已经在别的设备登录,请重新登录", @"your login account has been in other places") delegate:nil cancelButtonTitle:NSLocalizedString(@"确认", @"OK") otherButtonTitles:nil, nil];
                [alertView show];
                [viewController presentViewController:mianNav animated:YES completion:^{
                    [UserDefaultsHelper saveUid:@"" value:nil];
                    [UserDefaultsHelper saveToken:@"" value:nil];
                    [UserDefaultsHelper saveAutoLogIn:@"" value:@"NO"];
                }];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }
    
    
    
}
- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    
}
- (void)setServiceEvModel:(ServiceEvaluationModel *)serviceEvModel
{  
//    XLog(@"self.serviceEvModel.uid:%ld",self.serviceEvModel.uid);
    _serviceEvModel = serviceEvModel;
    
     [self.iv sd_setImageWithURL:[NSURL URLWithString:serviceEvModel.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    self.nickNamed.text = serviceEvModel.nickName;
    self.contentL.text = serviceEvModel.content;
    NSTimeInterval interval    = serviceEvModel.createtime/1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [formatter stringFromDate: date];
    self.timeL.text = currentDateStr;
    NSString *supports = [NSString stringWithFormat:@"%ld",serviceEvModel.supportcount];
    [self.giveLick setTitle:supports forState:UIControlStateNormal];
    
}
- (void)setShopEvaluationEvaluateInfosModel:(ShopEvaluationEvaluateInfosModel *)shopEvaluationEvaluateInfosModel
{
    _shopEvaluationEvaluateInfosModel = shopEvaluationEvaluateInfosModel;
    XLog(@"id:%ld",shopEvaluationEvaluateInfosModel.id);
    [self.iv sd_setImageWithURL:[NSURL URLWithString:shopEvaluationEvaluateInfosModel.headImg] placeholderImage:[UIImage imageNamed:@"headImg"]];
    self.nickNamed.text = shopEvaluationEvaluateInfosModel.nickName;
    self.contentL.text = shopEvaluationEvaluateInfosModel.content;
    NSTimeInterval interval    = shopEvaluationEvaluateInfosModel.createtime/1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [formatter stringFromDate: date];
    self.timeL.text = currentDateStr;
    NSString *supports = [NSString stringWithFormat:@"%ld",shopEvaluationEvaluateInfosModel.supportcount];
    [self.giveLick setTitle:supports forState:UIControlStateNormal];
    self.likeBtnConstraint.constant = [MCButton getWidthWithTitle:supports withFontSize:11]+35;
    self.images = [shopEvaluationEvaluateInfosModel.images componentsSeparatedByString:@","];
    NSInteger empty = [EmptyModel stringIsNullOrEmpty:shopEvaluationEvaluateInfosModel.images];
    
//    XLog(@"images:%@",images);
    if (self.images.count &&empty !=1) {
        [self.subIV sd_setImageWithURL:[NSURL URLWithString:self.images[0]] placeholderImage:[UIImage imageNamed:@""]];
        self.subViewConstraint.constant = 24;
    }else {
        self.subViewConstraint.constant = 0;
    }
//    NSInteger emptySupport = [EmptyModel stringIsNullOrEmpty:shopEvaluationEvaluateInfosModel.isSupport];
//            XLog(@"shopEvaluationEvaluateInfosModel.isSupport:%@",shopEvaluationEvaluateInfosModel.isSupport);
//    if (shopEvaluationEvaluateInfosModel.isSupport.length>0) {
        if (shopEvaluationEvaluateInfosModel.isSupport == 1) {
            self.giveLick.selected = YES;
            
        }else {
            self.giveLick.selected = NO;
        }
        
//    }else {
//        self.giveLick.selected = NO;
//    }
}
- (void)setShopEvaluationModel:(ShopEvaluationAssociationModel *)shopEvaluationModel
{
    _shopEvaluationModel = shopEvaluationModel;
    
    self.subTitle.text = shopEvaluationModel.industry;
}
- (IBAction)lookImageBtn:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(serviceEvaluationCell:withLookImage:)]) {
        [self.delegate serviceEvaluationCell:self withLookImage:self.images];
    }
}
#pragma mark-代理:点击投诉
- (IBAction)complaintBtn:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(serviceEvaluationCell:withShopEvaluationEvaluateInfosModel:withComplaintBtn:)]) {
        [self.delegate serviceEvaluationCell:self withShopEvaluationEvaluateInfosModel:self.shopEvaluationEvaluateInfosModel withComplaintBtn:sender];
    }
}
@end
